#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################


rm(list=ls())

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")


#'#
#'# Chargement des dependances
#'# 



#source(file.path(R_WORKING_DIRECTORY, "Utils/initCluster.R"))
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Comparaison avec le gps                                      ##
#'#                                                                                              ##
#'#################################################################################################


#'#
#'# Fichiers de travail
#'# 
MOBILE_WORKING_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")
MOBILE_DEST_DIR <- file.path("C:/UQAR/Recherche/Maitrise/Article/Manuscrit/Data/Mobile")

mobile.data <- loadData(MOBILE_WORKING_DIR, static=FALSE, raw=TRUE)
str(mobile.data)

lapply(mobile.data, saveArticleData)


#mobile.errors.filtered <- loadData(MOBILE_WORKING_DIR, static=FALSE)
mobileErrors <- loadErrorsFile(file.path(R_DATA_PATH, "Bylot", "mobile_errors.txt"))
setkey(mobileErrors, LC)

#'#
#'# Distributions cumulatives
#'#

locsclasses <- c("3", "2", "1", "0", "A", "B")

cumuldist <- list()
for (i in locsclasses) {
	cumuldist[i] <- list(ecdf(mobileErrors[i][, distance]))
}

localisations.props <- do.call(rbind, lapply(cumuldist, prop.between, props=c(250, 500, 1500)))
colnames(localisations.props) <- c("0-250", "250-500", "500-1500", ">1500")


colors <- rainbow(6)
names(colors) <- locsclasses
#plot(names(res), res, log="x")
plot(cumuldist[["3"]], log="x", xlim= c(10, 100000), do.points=F, verticals=T, col=colors[1], lwd=2)
for (i in 2: length(cumuldist)) {
	plot(cumuldist[[i]], add=TRUE, do.points=F, verticals=T, col=colors[i], lwd=2)
}
abline(v=250)
abline(v=500)
abline(v=1500)
legend("bottomright", col=colors, legend=names(cumuldist))

barplot(localisations.props, beside=TRUE)
barplot(t(localisations.props), beside=TRUE)


#'#
#'# Erreur globale statique et comparaison avec les classes Argos
#'# toutes classes de visibilite confondues
#'#

mobileError68 <- disp.boot(mobileErrors, stat.quantile,
		var=distance,
		quantileBy=quote(list(id, LC)),
		q=68,
		statBy=quote(list(LC)),
		key=c("LC"))
mobileErrorLong68 <- disp.boot(mobileErrors, stat.quantile,
		var=diffLong,
		quantileBy=quote(list(id, LC)),
		q=68,
		statBy=quote(list(LC)),
		key=c("LC"))
mobileErrorLat68 <- disp.boot(mobileErrors, stat.quantile,
		var=diffLat,
		quantileBy=quote(list(id, LC)),
		q=68,
		statBy=quote(list(LC)),
		key=c("LC"))




